package com.digitalchina.lxnsy.base;

import com.digitalchina.lxnsy.entity.CityList;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.raizlabs.android.dbflow.list.FlowQueryList;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.City_Table;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.County_Table;
import chihane.jdaddressselector.model.LatelyCity;
import chihane.jdaddressselector.model.LatelyCity_Table;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.base
 * 创建人：白彦东
 * 创建时间：2017/2/22 16:38
 * 修改备注：
 * ================================================
 */

public class CitySelectManager {

    private static final String BJ_CITY_CODE = "110100"; // 北京
    private static final String TJ_CITY_CODE = "120100"; // 天津
    private static final String SH_CITY_CODE = "310100"; // 上海
    private static final String NJ_CITY_CODE = "320100"; // 南京
    private static final String HZ_CITY_CODE = "330100"; // 杭州
    private static final String GZ_CITY_CODE = "440100"; // 广州
    private static final String SZ_CITY_CODE = "440300"; // 深圳
    private static final String CQ_CITY_CODE = "500100"; // 重庆
    private static final String CD_CITY_CODE = "510100"; // 成都
    private static final String WH_CITY_CODE = "420100"; // 武汉
    private static final String XA_CITY_CODE = "610100"; // 西安

    private CityList mCityList;
    private List<City> mCities;
    private List<City> mHotCities;

    public CitySelectManager(CityList cityList) {
        this.mCityList = cityList;
    }

    public void initCityData() {
        mCities = getCityList();
        Collections.sort(mCities, new CityComparator());
        mCityList.setList(mCities);
        mCityList.computePosition();
    }

    public List<City> initHotCities() {
        mHotCities = getHotCities();
        Collections.sort(mHotCities, new CityComparator());
        return mHotCities;
    }

    private List<City> getCityList() {

//        SQLite.update(City.class)
//                .set(City_Table.city_name.eq("北京市"), City_Table.city_name_pinyin.eq("BEIJING"))
//                .where(City_Table.city_code.is(BJ_CITY_CODE))
//                .async()
//                .execute();
//
//        SQLite.update(City.class)
//                .set(City_Table.city_name.eq("天津市"), City_Table.city_name_pinyin.eq("TIANJINSHI"))
//                .where(City_Table.city_code.is(TJ_CITY_CODE))
//                .async()
//                .execute();
//
//        SQLite.update(City.class)
//                .set(City_Table.city_name.eq("上海市"), City_Table.city_name_pinyin.eq("SHANGHAISHI"))
//                .where(City_Table.city_code.is(SH_CITY_CODE))
//                .async()
//                .execute();
//
//        SQLite.update(City.class)
//                .set(City_Table.city_name.eq("重庆市"), City_Table.city_name_pinyin.eq("CHONGQINGSHI"))
//                .where(City_Table.city_code.is(CQ_CITY_CODE))
//                .async()
//                .execute();

        FlowQueryList<City> cityFlowQueryList = SQLite.select()
                .from(City.class)
                .where(City_Table.city_code.notEq("110200"))
                .and(City_Table.city_code.notEq("120200"))
                .and(City_Table.city_code.notEq("310200"))
                .and(City_Table.city_code.notEq("419000"))
                .and(City_Table.city_code.notEq("429000"))
                .and(City_Table.city_code.notEq("469000"))
                .and(City_Table.city_code.notEq("500200"))
                .flowQueryList();
        return new ArrayList<>(cityFlowQueryList);
    }

    /**
     * 模糊查询城市名
     *
     * @param keyWord 关键字
     * @return
     */
    public List<City> searchCity(String keyWord) {
        keyWord = StringUtil.jointString("%", keyWord, "%");
        List<City> cities = SQLite.select()
                .from(City.class)
                .where(City_Table.city_name.like(keyWord))
                .or(City_Table.city_name_pinyin.like(keyWord))
                .and(City_Table.city_name.notEq("县"))
                .queryList();
        return new ArrayList<>(cities);
    }

    /**
     * 获取热门城市
     *
     * @return
     */
    private List<City> getHotCities() {
        List<City> hotCities = SQLite.select()
                .from(City.class)
                .where(City_Table.city_code.eq(BJ_CITY_CODE))
                .or(City_Table.city_code.eq(TJ_CITY_CODE))
                .or(City_Table.city_code.eq(SH_CITY_CODE))
                .or(City_Table.city_code.eq(NJ_CITY_CODE))
                .or(City_Table.city_code.eq(HZ_CITY_CODE))
                .or(City_Table.city_code.eq(GZ_CITY_CODE))
                .or(City_Table.city_code.eq(SZ_CITY_CODE))
                .or(City_Table.city_code.eq(CQ_CITY_CODE))
                .or(City_Table.city_code.eq(CD_CITY_CODE))
                .or(City_Table.city_code.eq(WH_CITY_CODE))
                .or(City_Table.city_code.eq(XA_CITY_CODE))
                .queryList();
        return new ArrayList<>(hotCities);
    }

    /**
     * sort by a-z
     */
    private class CityComparator implements Comparator<City> {
        @Override
        public int compare(City lhs, City rhs) {
            String a = lhs.getPinyin().substring(0, 1);
            String b = rhs.getPinyin().substring(0, 1);
            return a.compareTo(b);
        }
    }

    /**
     * 根据城市code查询城市名
     *
     * @param cityCode
     * @return
     */
    public City queryCityWithCode(String cityCode) {
        return SQLite.select()
                .from(City.class)
                .where(City_Table.city_code.eq(cityCode))
                .querySingle();
    }

    /**
     * 根据市code查询区
     */
    public List<County> queryCounties(String cityCode) {
        FlowQueryList<County> counties = SQLite.select()
                .from(County.class)
                .where(County_Table.city_code.eq(cityCode))
                .flowQueryList();
        return new ArrayList<>(counties);
    }

    /**
     * 根据市名称查询市Code
     */
    public City queryCityWithCityName(String cityName) {

        return SQLite.select()
                .from(City.class)
                .where(City_Table.city_name.eq(cityName))
                .querySingle();
    }

    /**
     * 存储最近浏览城市
     */
    public void saveLatelyCity(String cityCode, String cityName) {

        SQLite.insert(LatelyCity.class)
                .columns(LatelyCity_Table.city_code, LatelyCity_Table.city_name)
                .values(cityCode, cityName)

                .execute();
    }

    /**
     * 查询最近浏览城市
     */
    public List<LatelyCity> queryLatelyCity() {

        return SQLite.select(LatelyCity_Table.city_name, LatelyCity_Table.city_code)
                .distinct()
                .from(LatelyCity.class)
                .orderBy(LatelyCity_Table.id, false)
                .limit(3)
                .queryList();

    }

}
